﻿using DevExpress.Utils.Win;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraEditors.Popup;
using DevExpress.XtraGrid.Demos.util;
using DevExpress.XtraGrid.Editors;
using DevExpress.XtraLayout;
using Luna小店.service;
using Luna小店.service.workflow;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Luna小店.生产工单
{
    public partial class Frm创建样衣款式 : DevExpress.XtraEditors.XtraForm
    {
        public String SYTLE_FABRIC_ID { get; set; }
        public String 面料NA { get; set; }
        public String STYLE_DRESS_CATEGORY { get; set; }
        public String STYLE_PUBLISH_CATEGORY_CD { get; set; }
        public String STYLE_DESIGN_TYPE { get; set; }
        public String SYTLE_SEASON { get; set; }
        public String STYLE_NAME_CN { get; set; }
        public String SYTLE_YEAR { get; set; }
        public String STYLE_FIT_CD { get; set; }
        public Frm创建样衣款式()
        {
            InitializeComponent();
        }
        public Frm创建样衣款式(String STYLE_FIT_CD)
        {
            this.STYLE_FIT_CD = STYLE_FIT_CD;
            InitializeComponent();
        }
        //#region 参与者
        //private void searchLookUpEdit1_Popup(object sender, EventArgs e)
        //{
        //    //得到当前SearchLookUpEdit弹出窗体
        //    PopupSearchLookUpEditForm form = (sender as IPopupControl).PopupWindow as PopupSearchLookUpEditForm;
        //    SearchEditLookUpPopup popup = form.Controls.OfType<SearchEditLookUpPopup>().FirstOrDefault();
        //    LayoutControl layout = popup.Controls.OfType<LayoutControl>().FirstOrDefault();
        //    //如果窗体内空间没有确认按钮，则自定义确认simplebutton，取消simplebutton，选中结果label
        //    if (layout.Controls.OfType<Control>().Where(ct => ct.Name == "btOK").FirstOrDefault() == null)
        //    {
        //        //得到空的空间
        //        EmptySpaceItem a = layout.Items.Where(it => it.TypeName == "EmptySpaceItem").FirstOrDefault() as EmptySpaceItem;

        //        //得到取消按钮，重写点击事件
        //        Control clearBtn = layout.Controls.OfType<Control>().Where(ct => ct.Name == "btClear").FirstOrDefault();
        //        LayoutControlItem clearLCI = (LayoutControlItem)layout.GetItemByControl(clearBtn);
        //        clearBtn.Click += clearBtn_Click;

        //        //添加一个simplebutton控件(确认按钮)
        //        LayoutControlItem myLCI = (LayoutControlItem)clearLCI.Owner.CreateLayoutItem(clearLCI.Parent);
        //        myLCI.TextVisible = false;
        //        SimpleButton btOK = new SimpleButton() { Name = "btOK", Text = "确定" };
        //        btOK.Click += btOK_Click;
        //        myLCI.Control = btOK;
        //        myLCI.SizeConstraintsType = SizeConstraintsType.Custom;//控件的大小设置为自定义
        //        myLCI.MaxSize = clearLCI.MaxSize;
        //        myLCI.MinSize = clearLCI.MinSize;
        //        myLCI.Move(clearLCI, DevExpress.XtraLayout.Utils.InsertType.Left);
        //    }
        //}

        //public class LookUpMultSelectValues
        //{
        //    public string FindText { get; set; }
        //    public List<string> SelectedValues { get; set; }
        //    public List<string> SelectedDisplays { get; set; }
        //}
        ////参与者
        //private List<LookUpMultSelectValues> luValues = new List<LookUpMultSelectValues>();
        ////参与者去重
        //private List<string>[] GetLuValues()
        //{
        //    List<string> r = new List<string>();
        //    foreach (var a in luValues)
        //    {
        //        r.AddRange(a.SelectedValues);
        //    }

        //    List<string> b = new List<string>();
        //    foreach (var a in luValues)
        //    {
        //        b.AddRange(a.SelectedDisplays);
        //    }
        //    return new[] { r.Distinct().ToList<string>(), b.Distinct().ToList<string>() };
        //}

        ///// <summary>
        ///// 参与者清除按钮事件
        ///// </summary>
        ///// <param name="sender"></param>
        ///// <param name="e"></param>
        //private void clearBtn_Click(object sender, EventArgs e)
        //{
        //    this.luValues.Clear();
        //    searchLookUpEdit1.EditValue = null;
        //}
        ///// <summary>
        ///// 参与者确定按钮事件
        ///// </summary>
        ///// <param name="sender"></param>
        ///// <param name="e"></param>
        //private void btOK_Click(object sender, EventArgs e)
        //{
        //    searchLookUpEdit1.ClosePopup();
        //}


        //private void searchLookUpEdit1_Closed(object sender, ClosedEventArgs e)
        //{
        //    var re = GetLuValues();
        //    this.searchLookUpEdit1.EditValue = string.Join(",", re[1].ToArray());   //  name
        //    this.searchLookUpEdit1.ToolTip = string.Join(",", re[0].ToArray()); //  id
        //}

        //private void searchLookUpEdit1_CustomDisplayText(object sender, CustomDisplayTextEventArgs e)
        //{
        //    if (FunctionHelper.GetValue(e.Value).Contains(","))
        //    {
        //        e.DisplayText = e.Value.ToString();
        //    }
        //    else
        //    {
        //        var re = GetLuValues();
        //        if (re[0].Count == 1)
        //        {
        //            e.DisplayText = re[1].First();
        //        }
        //        else
        //        {
        //            e.DisplayText = "";
        //        }
        //    }
        //}

        //private void searchLookUpEdit1_EditValueChanged(object sender, EventArgs e)
        //{
        //    string estateId = FunctionHelper.GetValue(this.searchLookUpEdit1.EditValue);
        //    //if (estateId != "")
        //    //{
        //    //    this.searchLookUpEdit1.Properties.Buttons[1].Visible = true;
        //    //}
        //    //else
        //    //{
        //    //    this.searchLookUpEdit1.ToolTip = null;
        //    //    this.searchLookUpEdit1.Properties.View.ClearSelection();
        //    //    this.searchLookUpEdit1.Properties.Buttons[1].Visible = false;
        //    //}
        //}

        //private void searchLookUpEdit1View_SelectionChanged(object sender, DevExpress.Data.SelectionChangedEventArgs e)
        //{
        //    var a = this.searchLookUpEdit1.Properties.View.GetSelectedRows();
        //    var v = luValues.Find(b => b.FindText == this.searchLookUpEdit1.Properties.View.FindFilterText);
        //    if (v == null)
        //    {
        //        v = new LookUpMultSelectValues();
        //        v.FindText = this.searchLookUpEdit1.Properties.View.FindFilterText;
        //        v.SelectedValues = new List<string>();
        //        v.SelectedDisplays = new List<string>();
        //        luValues.Add(v);
        //    }
        //    if (a.Length > 0)
        //    {
        //        //新增状态时
        //        if (e.Action == CollectionChangeAction.Add)
        //        {
        //            foreach (int rowHandle in a)
        //            {
        //                var vv = this.searchLookUpEdit1.Properties.View.GetRowCellValue(rowHandle, "Id").ToString();//id 是 Value Member
        //                if (string.IsNullOrEmpty(v.SelectedValues.Find(b => b == vv)))
        //                {
        //                    v.SelectedValues.Add(vv);
        //                    v.SelectedDisplays.Add(this.searchLookUpEdit1.Properties.View.GetRowCellValue(rowHandle, "Name").ToString());//name 是 Display Member
        //                }

        //            }
        //        }
        //    }
        //    //删除状态时
        //    if (e.Action == CollectionChangeAction.Remove)
        //    {
        //        List<string> dels = new List<string>();
        //        List<string> deld = new List<string>();

        //        for (int i = 0; i < v.SelectedValues.Count; i++)
        //        {
        //            bool finded = false;
        //            foreach (int rowHandle in a)
        //            {
        //                var vv = this.searchLookUpEdit1.Properties.View.GetRowCellValue(rowHandle, "MATERIAL_ID").ToString();//id 是 Value Member
        //                var vn = this.searchLookUpEdit1.Properties.View.GetRowCellValue(rowHandle, "MATERIAL_NAME_CN").ToString();//name 是 Display Member
        //                if (v.SelectedValues[i] == vv)
        //                {
        //                    finded = true;
        //                    break;
        //                }
        //            }

        //            if (!finded)
        //            {
        //                dels.Add(v.SelectedValues[i]);
        //                deld.Add(v.SelectedDisplays[i]);
        //            }
        //        }

        //        v.SelectedValues.RemoveAll(b => dels.Contains(b));
        //        v.SelectedDisplays.RemoveAll(b => deld.Contains(b));

        //        for (int i = 0; i < luValues.Count; i++)
        //        {
        //            var ev = luValues[i];

        //            ev.SelectedValues.RemoveAll(b => dels.Contains(b));
        //            ev.SelectedDisplays.RemoveAll(b => deld.Contains(b));
        //        }
        //    }
        //}
        //#endregion
        #region 选择面料
        private void searchLookUpEdit1_Popup(object sender, EventArgs e)
        {
            //得到当前SearchLookUpEdit弹出窗体
            PopupSearchLookUpEditForm form = (sender as IPopupControl).PopupWindow as PopupSearchLookUpEditForm;
            SearchEditLookUpPopup popup = form.Controls.OfType<SearchEditLookUpPopup>().FirstOrDefault();
            LayoutControl layout = popup.Controls.OfType<LayoutControl>().FirstOrDefault();
            //如果窗体内空间没有确认按钮，则自定义确认simplebutton，取消simplebutton，选中结果label
            if (layout.Controls.OfType<Control>().Where(ct => ct.Name == "btOK").FirstOrDefault() == null)
            {
                //得到空的空间
                EmptySpaceItem a = layout.Items.Where(it => it.TypeName == "EmptySpaceItem").FirstOrDefault() as EmptySpaceItem;

                //得到取消按钮，重写点击事件
                Control clearBtn = layout.Controls.OfType<Control>().Where(ct => ct.Name == "btClear").FirstOrDefault();
                LayoutControlItem clearLCI = (LayoutControlItem)layout.GetItemByControl(clearBtn);
                clearBtn.Click += clearBtn_Click;

                //添加一个simplebutton控件(确认按钮)
                LayoutControlItem myLCI = (LayoutControlItem)clearLCI.Owner.CreateLayoutItem(clearLCI.Parent);
                myLCI.TextVisible = false;
            }
        }

        private void searchLookUpEdit1View_Click(object sender, EventArgs e)
        {
            var a = this.searchLookUpEdit1.Properties.View.GetSelectedRows();
            foreach (int rowHandle in a)
            {
                this.SYTLE_FABRIC_ID = this.SYTLE_FABRIC_ID + this.searchLookUpEdit1.Properties.View.GetRowCellValue(rowHandle, "MATERIAL_ID").ToString() + ",";
                this.面料NA = this.面料NA + this.searchLookUpEdit1.Properties.View.GetRowCellValue(rowHandle, "MATERIAL_NAME_CN").ToString() + ",";
                //this.dto无订单打印.materials_id //  no
                //    = this.searchLookUpEdit1.Properties.View.GetRowCellValue(rowHandle, "id").ToString();//id 是 Value Member
                //this.dto无订单打印.materials_cd //  no
                //    = this.searchLookUpEdit1.Properties.View.GetRowCellValue(rowHandle, "materialCode").ToString();//id 是 Value Member
                //this.dto无订单打印.materialComposition = this.searchLookUpEdit1.Properties.View.GetRowCellValue(rowHandle, "materialComposition").ToString();
                //this.dto无订单打印.materialNameCn = this.searchLookUpEdit1.Properties.View.GetRowCellValue(rowHandle, "materialNameCn").ToString();
            }
            //this.mianliaoid.Text = this.dto无订单打印.materials_id;
            //this.mianliaocd.Text = this.dto无订单打印.materials_cd;
            //this.chengfen.Text = this.dto无订单打印.materialComposition;
        }

        /// <summary>
        /// 清除按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void clearBtn_Click(object sender, EventArgs e)
        {
            this.searchLookUpEdit1.ToolTip = null;
            searchLookUpEdit1.EditValue = null;
        }
        private void searchLookUpEdit1_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
        {
            if (null != e.Value)
                e.DisplayText = this.面料NA;
        }
        #endregion


        public void Refresh面料()
        {
            this.searchLookUpEdit1.Properties.DataSource = 面料信息.面料信息s();
        }
        public class 面料信息
        {
            public String MATERIAL_ID { get; set; }//面料id
            public String MATERIAL_CODE { get; set; }//面料CD
            public String MATERIAL_NAME_CN { get; set; }//面料名称
            public String MATERIAL_COMPOSITION { get; set; }//面料成分
            public static List<面料信息> 面料信息s()
            {
                List<面料信息> lst = new List<面料信息>();
                foreach (DataRow dr in FabricService.GetAllMaterial().Rows)
                {
                    lst.Add(new 面料信息(dr));
                }
                return lst;
            }
            public 面料信息(DataRow dataRow)
            {
                this.MATERIAL_ID = dataRow["id"].ToString();
                this.MATERIAL_CODE = dataRow["materialCode"].ToString();
                this.MATERIAL_NAME_CN = dataRow["materialNameCn"].ToString();
                this.MATERIAL_COMPOSITION = dataRow["materialComposition"].ToString();
            }
        }

        private void Frm创建样衣款式_Load(object sender, EventArgs e)
        {
            Refresh面料();
            this.comboBoxSTYLE_DRESS_CATEGORY.DataSource = StyleService.GetSTYLE_DRESS_CATEGORY();
            this.comboBoxSTYLE_PUBLISH_CATEGORY_CD.DataSource = StyleService.GetPUBLISH_STYLE_CATEGORY();
            this.comboBoxSTYLE_DESIGN_TYPE.DataSource = StyleService.GetSTYLE_DESIGN_TYPE();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.STYLE_DRESS_CATEGORY = "STYLE_DRESS_CATEGORY-" + comboBoxSTYLE_DRESS_CATEGORY.SelectedValue.ToString();
        }

        private void comboBoxSTYLE_PUBLISH_CATEGORY_CD_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.STYLE_PUBLISH_CATEGORY_CD = "PUBLISH_STYLE_CATEGORY-" + comboBoxSTYLE_PUBLISH_CATEGORY_CD.SelectedValue.ToString();
        }

        private void comboBoxSTYLE_DESIGN_TYPE_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.STYLE_DESIGN_TYPE = "STYLE_DESIGN_TYPE-" + comboBoxSTYLE_DESIGN_TYPE.SelectedValue.ToString();
        }

        private void comboBoxSYTLE_SEASON_SelectedIndexChanged(object sender, EventArgs e)
        {
            if(this.comboBoxSYTLE_SEASON.Text=="春季")
            {
                this.SYTLE_SEASON = "SEASON-SEASON_10";
            }
            else
            {
                this.SYTLE_SEASON = "SEASON-SEASON_20";
            }
        }

        private void textBoxSTYLE_NAME_CN_TextChanged(object sender, EventArgs e)
        {
            this.STYLE_NAME_CN = this.textBoxSTYLE_NAME_CN.Text;
        }

        private void textBoxSYTLE_YEAR_TextChanged(object sender, EventArgs e)
        {
            this.SYTLE_YEAR = this.textBoxSYTLE_YEAR.Text;
        }

        private void simpleButton保存_Click(object sender, EventArgs e)
        {
            try
            {
                StyleService.CreateStyleByFit(this.STYLE_DRESS_CATEGORY, this.STYLE_DESIGN_TYPE, this.STYLE_PUBLISH_CATEGORY_CD, this.STYLE_NAME_CN, this.STYLE_FIT_CD, this.SYTLE_YEAR, this.SYTLE_SEASON, this.SYTLE_FABRIC_ID);
                MessageBox.Show("创建成功");
            }
            catch
            {
                MessageBox.Show("创建失败");
            }
        }
    }
}
